package Model;
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.w3c.dom.CharacterData;


public class tests {

    public static void main(String[] args) {
    	 
        try {
//        	getLanguageList();
//        	GetCategoryList(2);
//        	GetSubcategoryList(2,1);
//        	GetProductListByCategory(1,1);
//        	GetProductListBySubcategory(1,1,1);
//        	GetProductListByName("terven");
//		    GetProduct(1);
        } catch (Exception e) {
            System.out.println("Error:\n" + e);
        }
    }
    
    public static void getLanguageList() throws IOException, ParserConfigurationException, SAXException {

        URL url = new URL("http://eiffel.itba.edu.ar/hci/service/Common.groovy?method=GetLanguageList");
        URLConnection urlc = url.openConnection();
        urlc.setDoOutput(false);
        urlc.setAllowUserInteraction(false);
        
        BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
        String str;
        StringBuffer sb = new StringBuffer();
        while ((str = in.readLine()) != null) {
            sb.append(str);
            sb.append("\n");
        }
        in.close();

        String response = sb.toString();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(response));

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document dom = db.parse(is);

        String nodeName = "";
        String nodeValue = "";
        
        NodeList nl = dom.getElementsByTagName("language");
        for(int i=0; i<nl.getLength();i++) {
        	System.out.println("----------------------------");
        	System.out.println(nl.item(i).getAttributes().getNamedItem("id"));
        	
        	NodeList childs = nl.item(i).getChildNodes();
        	for(int j = 0; j < childs.getLength()/2; j++) {
        		nodeName = childs.item(j*2+1).getNodeName();
        		nodeValue = childs.item(j*2+1).getTextContent();
            	System.out.println(nodeName+": "+nodeValue);
        	}
        }
        return;
    }
    
    public static void GetCategoryList(int lan_id) throws IOException, ParserConfigurationException, SAXException {

        URL url = new URL("http://eiffel.itba.edu.ar/hci/service/Catalog.groovy?method=GetCategoryList&language_id="+lan_id);
        URLConnection urlc = url.openConnection();
        urlc.setDoOutput(false);
        urlc.setAllowUserInteraction(false);
        
        BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
        String str;
        StringBuffer sb = new StringBuffer();
        while ((str = in.readLine()) != null) {
            sb.append(str);
            sb.append("\n");
        }
        in.close();

        String response = sb.toString();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(response));
        Document dom = db.parse(is);

        String nodeName = "";
        String nodeValue = "";
        
        NodeList nl = dom.getElementsByTagName("category");
        for(int i=0; i<nl.getLength();i++) {
        	System.out.println("----------------------------");
        	System.out.println(nl.item(i).getAttributes().getNamedItem("id"));
        	
        	NodeList childs = nl.item(i).getChildNodes();
        	for(int j = 0; j < childs.getLength()/2; j++) {
        		nodeName = childs.item(j*2+1).getNodeName();
        		nodeValue = childs.item(j*2+1).getTextContent();
            	System.out.println(nodeName+": "+nodeValue);
        	}
        }
        
        return;
    }
    
    public static void GetSubcategoryList(int lan_id, int cat_id) throws IOException, ParserConfigurationException, SAXException {

        URL url = new URL("http://eiffel.itba.edu.ar/hci/service/Catalog.groovy?method=GetSubcategoryList&language_id="+lan_id+"&category_id="+cat_id);
        URLConnection urlc = url.openConnection();
        urlc.setDoOutput(false);
        urlc.setAllowUserInteraction(false);
        
        BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
        String str;
        StringBuffer sb = new StringBuffer();
        while ((str = in.readLine()) != null) {
            sb.append(str);
            sb.append("\n");
        }
        in.close();

        String response = sb.toString();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(response));
        Document dom = db.parse(is);
        
        String nodeName = "";
        String nodeValue = "";
        
        NodeList nl = dom.getElementsByTagName("subcategory");
        for(int i=0; i<nl.getLength();i++) {
        	System.out.println("----------------------------");
        	System.out.println(nl.item(i).getAttributes().getNamedItem("id"));
        	
        	NodeList childs = nl.item(i).getChildNodes();
        	for(int j = 0; j < childs.getLength()/2; j++) {
        		nodeName = childs.item(j*2+1).getNodeName();
        		nodeValue = childs.item(j*2+1).getTextContent();
            	System.out.println(nodeName+": "+nodeValue);
        	}
        }        
        return;
    }
    
    public static void GetProductListByCategory(int lan_id, int cat_id) throws IOException, ParserConfigurationException, SAXException {

        URL url = new URL("http://eiffel.itba.edu.ar/hci/service/Catalog.groovy?method=GetProductListByCategory&language_id="+lan_id+"&category_id="+cat_id);
        URLConnection urlc = url.openConnection();
        urlc.setDoOutput(false);
        urlc.setAllowUserInteraction(false);
        
        BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
        String str;
        StringBuffer sb = new StringBuffer();
        while ((str = in.readLine()) != null) {
            sb.append(str);
            sb.append("\n");
        }
        in.close();

        String response = sb.toString();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(response));
        Document dom = db.parse(is);
     
        String nodeName = "";
        String nodeValue = "";
        
        NodeList nl = dom.getElementsByTagName("product");
        for(int i=0; i<nl.getLength();i++) {
        	System.out.println("----------------------------");
        	System.out.println(nl.item(i).getAttributes().getNamedItem("id"));
        	
        	NodeList childs = nl.item(i).getChildNodes();
        	for(int j = 0; j < childs.getLength()/2; j++) {
        		nodeName = childs.item(j*2+1).getNodeName();
        		nodeValue = childs.item(j*2+1).getTextContent();
            	System.out.println(nodeName+": "+nodeValue);
        	}
        }
        
        return;
    }
    
    public static void GetProductListBySubcategory(int lan_id, int cat_id, int subcat_id) throws IOException, ParserConfigurationException, SAXException {

        URL url = new URL("http://eiffel.itba.edu.ar/hci/service/Catalog.groovy?method=GetProductListBySubcategory&language_id="+lan_id+"&category_id="+cat_id+"&subcategory_id="+subcat_id);
        URLConnection urlc = url.openConnection();
        urlc.setDoOutput(false);
        urlc.setAllowUserInteraction(false);
        
        BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
        String str;
        StringBuffer sb = new StringBuffer();
        while ((str = in.readLine()) != null) {
            sb.append(str);
            sb.append("\n");
        }
        in.close();

        String response = sb.toString();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(response));
        Document dom = db.parse(is);
        
        String nodeName = "";
        String nodeValue = "";
        NodeList nl = dom.getElementsByTagName("product");
        for(int i=0; i<nl.getLength();i++) {
        	System.out.println("----------------------------");
        	System.out.println(nl.item(i).getAttributes().getNamedItem("id"));
        	
        	NodeList childs = nl.item(i).getChildNodes();
        	for(int j = 0; j < childs.getLength()/2; j++) {
        		nodeName = childs.item(j*2+1).getNodeName();
        		nodeValue = childs.item(j*2+1).getTextContent();
            	System.out.println(nodeName+": "+nodeValue);
        	}
        }
        
        return;
    }
    
    public static void GetProductListByName(String criteria) throws IOException, ParserConfigurationException, SAXException {

        URL url = new URL("http://eiffel.itba.edu.ar/hci/service/Catalog.groovy?method=GetProductListByName&criteria="+criteria);
        URLConnection urlc = url.openConnection();
        urlc.setDoOutput(false);
        urlc.setAllowUserInteraction(false);
        
        BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
        String str;
        StringBuffer sb = new StringBuffer();
        while ((str = in.readLine()) != null) {
            sb.append(str);
            sb.append("\n");
        }
        in.close();

        String response = sb.toString();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(response));
        Document dom = db.parse(is);
        
        String nodeName = "";
        String nodeValue = "";
        NodeList nl = dom.getElementsByTagName("product");
        for(int i=0; i<nl.getLength();i++) {
        	System.out.println("----------------------------");
        	System.out.println(nl.item(i).getAttributes().getNamedItem("id"));
        	
        	NodeList childs = nl.item(i).getChildNodes();
        	for(int j = 0; j < childs.getLength()/2; j++) {
        		nodeName = childs.item(j*2+1).getNodeName();
        		nodeValue = childs.item(j*2+1).getTextContent();
            	System.out.println(nodeName+": "+nodeValue);
        	}
        }
        
        return;
    }
    
    public static void GetProduct(int product_id) throws IOException, ParserConfigurationException, SAXException {

        URL url = new URL("http://eiffel.itba.edu.ar/hci/service/Catalog.groovy?method=GetProduct&product_id="+product_id);
        URLConnection urlc = url.openConnection();
        urlc.setDoOutput(false);
        urlc.setAllowUserInteraction(false);
        
        BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
        
        String str;
        StringBuffer sb = new StringBuffer();
        while ((str = in.readLine()) != null) {
            sb.append(str);
            sb.append("\n");
        }
        in.close();

        String response = sb.toString();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(response));

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document dom = db.parse(is);

        String nodeName = "";
        String nodeValue = "";

        NodeList nl = dom.getElementsByTagName("product").item(0).getChildNodes();
        for(int i=0; i<nl.getLength()/2;i++) {
        	nodeName = nl.item(i*2+1).getNodeName();
        	nodeValue = nl.item(i*2+1).getFirstChild().getNodeValue();
        	System.out.println(nodeName + ": "+ nodeValue);
        }
        
        return;
    }
    
    /**
    * Constructs a Document object by reading from an input stream.
    */
        public static Document parse (InputStream is) {
            Document ret = null;
            DocumentBuilderFactory domFactory;
            DocumentBuilder builder;

            try {
                domFactory = DocumentBuilderFactory.newInstance();
                domFactory.setValidating(false);
                domFactory.setNamespaceAware(false);
                builder = domFactory.newDocumentBuilder();

                ret = builder.parse(is);
            }
            catch (Exception ex) {
                System.err.println("unable to load XML: " + ex);
            }
            return ret;
        }
}
